/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * A small mine type bomb for testing purposes
 */

#include "GameBombSmallMine.h"
#include "../../../GameEngine.h"
#include "../../../GameCamera.h"
#include "../../../objects/physicobjects/GamePhysicObjectEngine.h"
#include "../../../misc/GameWorldModule.h"
#include "../../../layers/LayerGame.h"
#include "../../../effects/GameEffects.h"
#include "../../../../Resources.h"
#include "../../../../Common.h"

using namespace cocos2d;

const float SPRITE_TIME = 1.0f;
const float EXPLOSION_FORCE = 0.04f;

// default constructor
GameBombSmallMine::GameBombSmallMine( b2World *world, cocos2d::CCLayer *layer, GameCamera *camera, b2Vec2 pos )
	: GameBomb( world, layer, camera ), mSpriteCounter( SPRITE_TIME ) {
	// init sprites
	CCSprite *tmpSprites[ 2 ];
	tmpSprites[ 0 ] = CCSprite::spriteWithFile( getResource( Textures::GAME_BOMB_SMALLMINE_A ) );
	tmpSprites[ 1 ] = CCSprite::spriteWithFile( getResource( Textures::GAME_BOMB_SMALLMINE_B ) );
	for( unsigned int i = 0; i < 2; ++i ) {
		tmpSprites[ i ]->setScale( RATEX * 0.5f );
		layer->addChild( tmpSprites[ i ], Z_LAYER_GAME_FRONT );
		mSprites.push_back( tmpSprites[ i ] );
	}
	mSprites.at( 1 )->setIsVisible( false );

	// create physics body
	ObjectProperties properties( 0.01f, 0.7f, 0.2f );
	b2Body *mineBody = createBox( false, 1.25f, 1.2f, pos.x, pos.y, properties );
	mBodies->push_back( mineBody );
}

// default destructor
GameBombSmallMine::~GameBombSmallMine() {
	// release sprites
	for( unsigned int i = 0; i < mSprites.size(); ++i )
		mGameLayer->removeChild( mSprites.at( i ), true );
}

// update function
void GameBombSmallMine::update( cocos2d::ccTime dt ) {
	GameBomb::update( dt );
	
	// get body position & rotation
	b2Vec2 myWorldPos = mBodies->at( 0 )->GetPosition();
	cocos2d::CCPoint myScreenPos = mGameCamera->convertWorldToScreen( myWorldPos.x, myWorldPos.y );
	float myRotation = mBodies->at( 0 )->GetAngle() * -180.0f / 3.14159f;
	// set sprite position & rotation
	for( unsigned int i = 0; i < mSprites.size(); ++i ) {
		mSprites.at( i )->setPosition( myScreenPos );
		mSprites.at( i )->setRotation( myRotation );
	}

	// switch visible sprite if needed
	mSpriteCounter -= dt;
	if( mSpriteCounter < 0 ) {
		mSpriteCounter = SPRITE_TIME;
		mSprites.at( 0 )->setIsVisible( ! mSprites.at( 0 )->getIsVisible() );
		mSprites.at( 1 )->setIsVisible( ! mSprites.at( 1 )->getIsVisible() );
	}
}

// detonate bomb
void GameBombSmallMine::detonate() {
	// show effect
	b2Vec2 myWorldPos = mBodies->at( 0 )->GetPosition();
	cocos2d::CCPoint myScreenPos = mGameCamera->convertWorldToScreen( myWorldPos.x, myWorldPos.y );
	CCParticleSystem *s = GameEffects::createSmallExplosion( myScreenPos.x, myScreenPos.y );
	mGameLayer->addChild( s, Z_LAYER_GAME_EFFECTS );

	// cause physics explosion
	GameWorldModule::causePhysicWorldExplosion( mGameWorld, 
		dynamic_cast< LayerGame* >( mGameLayer )->getGameEngine()->getPhysicObjectEngine()->getPhysicObjects(), 
		mBodies->at( 0 )->GetPosition(), EXPLOSION_FORCE );

	// set this object to be removed
	mCanBeRemoved = true;
}

// get the main type of the object
PhysicObjectItems GameBombSmallMine::getObjectType() {
	return BOMB_SMALL_MINE;
}
